﻿using System;
using System.Linq;
using System.Linq.Expressions;
using System.Web.Security;
using System.Web.UI;
using SBACN.WebSite.Cache;
using SBACN.WebSite.Code;
using SBACN.WebSite.Models;
using SBACN.WebSite.UC;

namespace SBACN.WebSite
{
	public partial class Index : BasePage
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			if(!Page.IsPostBack)
			{
				InitCtrl();
			}
		}

		private void InitCtrl()
		{
			#region 图片新闻
			TB_SystemConfig config = SystemConfigCache.Instance.List.Where(p => p.Type == "00090005" && p.Code == p.Type).FirstOrDefault();
			int picNewsCount = 5;
			if(config != null && config.Status )
			{
				int.TryParse( config.Value,out picNewsCount);
			}
			Expression<Func<TB_News, bool>> f = (p => p.IsImageNews && !p.IsEn && p.MainImageUrl.Length > 0 && p.Type != "00100004");
			Expression<Func<TB_News, DateTime>> o = (p => p.CreateDate);
			Expression<Func<TB_News, int>> t = (p => p.ID);

			int newsCount = 0;
			var newsList = DAL.DALHelper.Instance.GetList(f, o, false, t, false, picNewsCount, 1, out newsCount);

			pic_container.Visible = newsList.Count > 0;

			for (int i = 1; i <= newsList.Count; i++)
			{
				numCtrl.InnerHtml += "<a>" + i + "</a>";
			}

			this.rpt_PicNews.DataSource = newsList;
			this.rpt_PicNews.DataBind();
			#endregion

			#region 要闻导读
			f = (p => p.Type == "00020001" && !p.IsEn);
			newsList = DAL.DALHelper.Instance.GetList(f, o, false, t, false, 4, 1, out newsCount);
			this.rpt_impNews.DataSource = newsList;
			this.rpt_impNews.DataBind();
			#endregion

			#region 会员风采
			f = (p => p.Type.StartsWith("0006") && p.Type.Length == 8 && !p.IsEn);
			newsList = DAL.DALHelper.Instance.GetList(f, o, false, t, false, 5, 1, out newsCount);
			this.rpt_Member.DataSource = newsList;
			this.rpt_Member.DataBind();
			#endregion

			#region 金融资讯
			f = (p => p.Type.StartsWith("0004") && p.Type.Length == 8 && !p.IsEn);
			newsList = DAL.DALHelper.Instance.GetList(f, o, false, t, false, 5, 1, out newsCount);
			this.rpt_Finace.DataSource = newsList;
			this.rpt_Finace.DataBind();
			#endregion

			#region 滚动公告
			config = SystemConfigCache.Instance.List.Where(p => p.Type == "00090006" && p.Code == p.Type).FirstOrDefault();
			int autoScrollNum = 5;
			if (config != null && config.Status )
			{
				int.TryParse(config.Value, out autoScrollNum);
			}
			f = (p => p.IsAutoScorllNews && !p.IsEn);
			newsList = DAL.DALHelper.Instance.GetList(f, o, false, t, false, autoScrollNum, 1, out newsCount);
			this.rpt_AutoScoll.DataSource = newsList;
			this.rpt_AutoScoll.DataBind();
			#endregion

			#region 子系统外链
			//银团
			TB_SystemMap map = SystemMapCache.Instance["00050004"];
			if (map != null)
			{
				linkBank.HRef = map.Status ? (string.IsNullOrWhiteSpace(map.CURL) ? "javascript:void(0)" : map.CURL) : "javascript:void(0)";
				linkBank.Target = map.Status && !string.IsNullOrWhiteSpace(map.CURL) ? "_blank" : "_self";
			}
			//数据库
			map = SystemMapCache.Instance["00050005"];
			if (map != null)
			{
				linkDB.HRef = map.Status ? (string.IsNullOrWhiteSpace(map.CURL) ? "javascript:void(0)" : map.CURL) : "javascript:void(0)";
				linkDB.Target = map.Status && !string.IsNullOrWhiteSpace(map.CURL) ? "_blank" : "_self";
			}
			//赴美
			map = SystemMapCache.Instance["00050006"];
			if (map != null)
			{
				linkAm.HRef = map.Status ? (string.IsNullOrWhiteSpace(map.CURL) ? "javascript:void(0)" : map.CURL) : "javascript:void(0)";
				linkAm.Target = map.Status && !string.IsNullOrWhiteSpace(map.CURL) ? "_blank" : "_self";
			}
			//小微
			map = SystemMapCache.Instance["00070002"];
			if (map != null)
			{
				linkSmall.HRef = map.Status ? (string.IsNullOrWhiteSpace(map.CURL) ? "javascript:void(0)" : map.CURL) : "javascript:void(0)";
				linkSmall.Target = map.Status && !string.IsNullOrWhiteSpace(map.CURL) ? "_blank" : "_self";
			}
			//微博
			map = SystemMapCache.Instance["00090004"];
			if (map != null)
			{
				// 当不启用时跳转到指定提示页面
				linkSmallBlog.HRef = map.Status ? (string.IsNullOrWhiteSpace(map.CURL) ? "/MiniBlogMessage.aspx" : map.CURL) : "/MiniBlogMessage.aspx";
				//linkSmallBlog.Target = map.Status && !string.IsNullOrWhiteSpace(map.CURL) ? "_blank" : "_blank";
				linkSmallBlog.InnerHtml = "<img src=\"/images/img_miniblog" + (map.Status ? "1" : "0") + ".jpg\" width=\"220\" height=\"74\" border=\"0\" />";
			}
			#endregion

			#region 专题活动
			this.rpt_Spa.Visible = false;
			if(SystemMapCache.Instance["00100004"] != null && SystemMapCache.Instance["00100004"].Status)
			{
				f = (p => p.Type == "00100004" && p.MainImageUrl.Length > 0);
				newsList = DAL.DALHelper.Instance.GetList(f, o, false, t, false);
				this.rpt_Spa.DataSource = newsList;
				this.rpt_Spa.DataBind();
				this.rpt_Spa.Visible = newsList.Count > 0;
			}
			#endregion

			TB_User user = Session["UserInfo"] as TB_User;
			InitLoginInfo(user);
		}

		protected void btn_Login_Click(object sender, EventArgs e)
		{
			string name = txt_UserName.Text.Trim();
			string password = hid_Password.Value.Trim().ToLower();
			if(string.IsNullOrEmpty(name))
			{
				this.lbl_Msg.Text = "用户名不能为空!";
				return;
			}
			string emptyMD5 = FormsAuthentication.HashPasswordForStoringInConfigFile(string.Empty,"MD5").ToLower();
			if (string.IsNullOrWhiteSpace(password) || password == emptyMD5)
			{
				this.lbl_Msg.Text = "密码不能为空!";
				return;
			}
			
			password = FormsAuthentication.HashPasswordForStoringInConfigFile(password,"MD5").ToLower();
			TB_User user = DAL.DALHelper.Instance.GetObject<TB_User>("UserName", name);
			if(user != null && user.Password == password)
			{
				if (user.Status && UserRightCache.Instance[user.UserName].Count > 0)
				{
					user.LastLoginIP = user.LoginIP;
					user.LastLoginTime = user.LoginTime;
					user.LoginIP = BasePage.GetRequestIP(Request);
					user.LoginTime = DateTime.Now;
					user.LoginCount++;
					DAL.DALHelper.Instance.Update(user);
					DAL.DALHelper.Instance.SaveChanges();
					Session["UserInfo"] = user;
					this.lbl_Msg.Text = string.Empty;

					InitLoginInfo(user);

					var header = this.Master.FindControl("Header1") as Header;

					if (header != null)
					{
						header.InitCtrl();
					}
				}
				else
				{
					this.lbl_Msg.Text = "禁止登录!";
				}
			}
			else
			{
				this.lbl_Msg.Text = "用户名、密码不匹配!";
			}
		}

		private void InitLoginInfo(TB_User user)
		{
			if(user != null)
			{
				this.lbl_UserName.Text = user.NickName ?? user.UserName;
				this.lbl_LoginIP.Text = user.LoginIP;
				DateTime dtLastLogin = user.LoginTime ?? DateTime.MinValue;
				this.lbl_LoginTime.Text = dtLastLogin == DateTime.MinValue ? "首次访问本系统" : dtLastLogin.ToString("yyyy-MM-dd HH:mm");
				TB_Member tm = DAL.DALHelper.Instance.GetObject<TB_Member>("ID", user.MemberID);
				if (tm != null)
				{
					this.lbl_MemberName.Text = tm.Name;
				}
				else
				{
					this.lbl_MemberName.Text = "后台系统用户";
				}

				this.tr_LoginTime.Visible = true;
				this.tr_MemberName.Visible = true;
				this.tr_UserName.Visible = true;
				this.tr_LoginIP.Visible = true;
				this.tr_Pwd.Visible = true;
				this.tr_NameInput.Visible = false;
				this.tr_PwdInput.Visible = false;
				this.tr_Login.Visible = false;
			}
			else
			{
				this.tr_LoginTime.Visible = false;
				this.tr_MemberName.Visible = false;
				this.tr_UserName.Visible = false;
				this.tr_LoginIP.Visible = false;
				this.tr_Pwd.Visible = false;
				this.tr_NameInput.Visible = true;
				this.tr_PwdInput.Visible = true;
				this.tr_Login.Visible = true;
			}
			
		}
	}
}